package com.bishe.tax.controller.sys;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxNews;
import com.bishe.tax.service.SysMenuService;
import com.bishe.tax.service.SysUserService;
import com.bishe.tax.service.TaxNewsService;
import com.bishe.tax.utils.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: xhl
 * @Date: 2019/6/3
 * @Description:
 */
@Controller
@RequestMapping("sys/index")
public class MainController {

    @Autowired
    private TaxNewsService newsService;
    @Autowired
    private SysMenuService menuService;
    @Autowired
    private SysUserService userService;

    @GetMapping("/")
    public String index(HttpSession session, Model model) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        model.addAttribute("meuns", menuService.selectMenusByUser(user));
        model.addAttribute("count", 1);
        return "home/index";
    }

    @GetMapping("/main")
    public String main(ModelMap mmp) {
        return newsList(1, mmp);
    }

    @GetMapping("/getMenus")
    @ResponseBody
    public AjaxResult getMenus(HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        return AjaxResult.success("", menuService.selectMenusByUser(user));
    }

    @GetMapping("/changePwd")
    public String pwd(HttpSession session, Model model) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        model.addAttribute("user", user);
        return "home/changepwd";
    }

    @PostMapping("/changePwd")
    @ResponseBody
    public AjaxResult changePwd(@RequestParam("oldpwd") String oldpwd, @RequestParam("pwd") String pwd,
                                HttpSession session) {
        SysUser user = (SysUser) session.getAttribute("sysUser");
        String encodeStr = DigestUtils.md5Hex(oldpwd);
        if (encodeStr.equalsIgnoreCase(user.getPassword())) {
            user.setPassword(pwd);
        } else {
            return AjaxResult.error("旧密码错误");
        }
        return AjaxResult.success("", userService.updateUser(user));
    }


    @GetMapping("/news/{id}")
    public String news(@PathVariable("id") String id, ModelMap mmp) {
        TaxNews news = newsService.selectNewsById(Integer.valueOf(id));
        mmp.put("news", news);
        return "home/news";
    }

    @GetMapping("/newsList")
    public String newsList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, ModelMap mmp) {
        PageHelper.startPage(pageNum, 10);
        List<TaxNews> newsList = newsService.selectNewsList(new TaxNews());
        PageInfo<TaxNews> page = new PageInfo<>(newsList);
        mmp.put("newsList", page.getList());
        mmp.put("page", page);
        return "home/main";
    }

}
